package tq.com.easysurfacecapture;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.SizeF;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import tq.com.easysurfacecapture.Inner.TQMediaRecordHelper;
import tq.com.easysurfacecapture.Inner.TQPathHelper;
import tq.com.easysurfacecapture.Inner.TQPictureHold;

public class TQCaputreView extends SurfaceView implements Runnable, SurfaceHolder.Callback {
    private Context context;
    private SurfaceHolder surfaceHolder;
    private Boolean isStopRefresh = false;
    private TQPathHelper pathHelper = new TQPathHelper();
    private TQPictureHold mPictureHold = null;

    private PointF mMoveStart = new PointF(0, 0);
    private PointF mMoveDistance = new PointF(0, 0);
    private PointF offset = new PointF(0, 0);

    private Paint paint = new Paint();

    private int width, height = 0;

    static private int DIRECTOR_NONE = 0x00;
    static private int DIRECTOR_X = 0x01;
    static private int DIRECTOR_Y =0x02;
    private int mvDirect = DIRECTOR_NONE;
    public TQCaputreView(Context context){
        super(context);
        init(context);
    }
    public TQCaputreView(Context context, AttributeSet attrs){
        super(context, attrs);
        init(context);
    }
    public TQCaputreView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);

        init(context);
    }

    private void init(Context context){
        this.context = context;
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
    }

    public void setPicture(String pPicture){
        mPictureHold = new TQPictureHold(pPicture);
        mPictureHold.setParentSize(new SizeF(getMeasuredWidth(), getMeasuredHeight()));
        invalidate();
    }
    public void setPicture(Bitmap pDrawable){
        mPictureHold = new TQPictureHold(pDrawable);
        mPictureHold.setParentSize(new SizeF(getMeasuredWidth(), getMeasuredHeight()));
        invalidate();
    }

    static public int ACTION_PAINT_PATH = 0x00;
    static public int ACTION_SCROLL = 0x01;
    static public int ACTION_MOVE_PIC = 0x02;

    private int action = ACTION_PAINT_PATH;
    public int getAction(){
        return action;
    }
    public void setAction(int action){
        this.action = action;
    }

    public void surfaceCreated(SurfaceHolder holder){
        paint.setStrokeWidth(4);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStyle(Paint.Style.STROKE);

        isStopRefresh = false;
        holder.setKeepScreenOn(true);
        new Thread(this).start();
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height){
        this.width = width;
        this.height = height;

    }
    public void surfaceDestroyed(SurfaceHolder holder){
        isStopRefresh = true;
    }

    public void run(){
        while(!isStopRefresh){
            try {
                draw();
                Thread.sleep(50);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void removeLast(){
        pathHelper.cancelLast();
        invalidate();
    }

    public void draw(){
        Canvas c = surfaceHolder.lockCanvas();
        mkDraw(c);
        surfaceHolder.unlockCanvasAndPost(c);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(mPictureHold != null){
            mPictureHold.setParentSize(new SizeF(getMeasuredWidth(), getMeasuredHeight()));
        }

    }

    public Bitmap drawImage(){
        int w = getWidth();
        int h = getHeight();
        if(w == 0 || h == 0) return null;

        if(w % 2 == 1){
            w += 1;
        }

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        mkDraw(canvas);
        return bitmap;
    }

    private void mkDraw(Canvas canvas){
        PointF drawOffset = new PointF(offset.x, offset.y);
        drawOffset.offset(mMoveDistance.x, mMoveDistance.y);
        if(drawOffset.x < 0){
            drawOffset.x = 0;
        }
        if(drawOffset.y < 0){
            drawOffset.y = 0;
        }

        canvas.drawColor(Color.WHITE);

        if(mPictureHold != null){
            mPictureHold.draw(canvas, paint, new PointF(0, -drawOffset.y));
        }
        pathHelper.drawPath(canvas, paint, new PointF(0, -drawOffset.y));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        PointF pos = new PointF(event.getX(), event.getY());
        pos.offset(offset.x, offset.y);

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:{
                if(action == ACTION_SCROLL){
                    mMoveStart = pos;
                }else if(action == ACTION_MOVE_PIC){

                }else {
                    pathHelper.newPath(pos);
                }
            }break;
            case MotionEvent.ACTION_MOVE:{
                if(action == ACTION_SCROLL){
                    mkDistance(pos);

                }else if(action == ACTION_MOVE_PIC){

                }else {
                    pathHelper.addCurrentPoint(pos);
                }
            }break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:{
                if(action == ACTION_SCROLL){
                    mkDistance(pos);
                    mkOffset(mMoveDistance);
                    mvDirect = DIRECTOR_NONE;

                }else if(action == ACTION_MOVE_PIC){

                }else {
                    pathHelper.endCurrentPath(pos);
                }
            }break;
            default:{

            }
        }
        super.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    private void mkOffset(PointF offset){
        this.offset.offset(offset.x, offset.y);
        if(this.offset.x < 0){
            this.offset.x = 0;
        }

        if(this.offset.y < 0){
            this.offset.y = 0;
        }
        mMoveDistance.x = 0; mMoveDistance.y = 0;
    }
    private void mkDirector(PointF pos){
        if(mvDirect != DIRECTOR_NONE){
            return;
        }

        PointF offset = new PointF(pos.x - mMoveStart.x, pos.y - mMoveStart.y);
        if(Math.abs(offset.x) > Math.abs(offset.y)){
            mvDirect = DIRECTOR_X;
        }else{
            mvDirect = DIRECTOR_Y;
        }
    }

    private void mkDistance(PointF pos){
        if(mvDirect == DIRECTOR_NONE){
            mkDirector(pos);
        }

        if(mvDirect == DIRECTOR_X){
            mMoveDistance = new PointF(mMoveStart.x - pos.x, 0);
        }else{
            mMoveDistance = new PointF(0, mMoveStart.y -pos.y);
        }
    }
}
